﻿#pragma once

#include  "qcontrol.h"


namespace micro
{


namespace gui
{

class QButton: public QWindow
{
protected:
    QString	m_label;
    QRect	m_rect;
public:
    QButton( const QString  label = _T( "" ) );
    virtual ~QButton( void );

    virtual bool Create( QWindow * parent, int id = 0, UINT style = 0 );
    virtual bool Create( const QString & label, QRect rect , QWindow * parent,   int id = 0, UINT style = 0 );


    // Attributes
    virtual LPCTSTR GetWndClassName()
    {
        return WC_LINK;
    }


    // Simulate a click on the button.
    inline void Click()
    {
        ASSERT( _window ); // Must be valid.

        // Send the BM_CLICK message.
        SendMessage( BM_CLICK );
    }

    // Get the button checked state.
    inline bool GetCheck() const
    {
        ASSERT( _window ); // Must be valid.

        // Return the checked state.
        return BST_CHECKED == ::SendMessage( _window, BM_GETCHECK, 0, 0 );
    }

    // Set the button checked state.
    inline void SetCheck( UINT nCheck )
    {
        ASSERT( _window ); // Must be valid.

        // Set the checked state.
        SendMessage( BM_SETCHECK, nCheck );
    }

    // Get the button icon.
    inline HICON GetIcon() const
    {
        ASSERT( _window ); // Must be valid.

        // Return the icon.
        return ( HICON )::SendMessage( _window, BM_GETIMAGE, IMAGE_ICON, 0 );
    }

    // Set the button icon.
    inline HICON SetIcon( HICON hIcon )
    {
        ASSERT( _window ); // Must be valid.

        // Set the new icon, returning the old.
        return ( HICON )SendMessage( BM_SETIMAGE, IMAGE_ICON, ( LPARAM )hIcon );
    }

    // Get the button bitmap.
    inline HBITMAP GetBitmap() const
    {
        ASSERT( _window ); // Must be valid.

        // Return the bitmap.
        return ( HBITMAP )::SendMessage( _window, BM_GETIMAGE, IMAGE_BITMAP, 0 );
    }

    // Set the button bitmap.
    inline HBITMAP SetBitmap( HBITMAP hBitmap )
    {
        ASSERT( _window ); // Must be valid.

        // Set the new bitmap, returning the old.
        return ( HBITMAP )SendMessage( BM_SETIMAGE, IMAGE_BITMAP, ( LPARAM )hBitmap );
    }

    // Get the button state.
    inline UINT GetState() const
    {
        ASSERT( _window ); // Must be valid.

        // Return the button state.
        return ( UINT )::SendMessage( _window, BM_GETSTATE, 0, 0 );
    }

    // Set the button state.
    inline void SetState( UINT nState )
    {
        ASSERT( _window ); // Must be valid.

        // Set the new state.
        SendMessage( BM_SETSTATE, nState );
    }

    // Set the button style.
    inline void SetStyle( UINT nStyle )
    {
        ASSERT( _window ); // Must be valid.

        // Set the new style.
        SendMessage( BM_SETSTYLE, nStyle );
    }

protected:
    // Reflected WM_COMMAND notification handlers.
    virtual LRESULT OnClicked(   BOOL & bNotifyParent )
    {
        return 0;
    }
    virtual bool OnSetFocus(  HWND hwnd )
    {
        return false;
    }
    virtual bool OnKillFocus( HWND hwnd )
    {
        return false;
    }

    // Handle the reflected WM_COMMAND messages.
    virtual LRESULT OnReflectedCommand( UINT nNotifyCode, BOOL & bNotifyParent )
    {
        switch ( nNotifyCode )
        {
            case	BN_CLICKED:
                return OnClicked( bNotifyParent );

            case	BN_SETFOCUS:
                return OnSetFocus( nullptr );

            case	BN_KILLFOCUS:
                return OnKillFocus( nullptr );
        }

        return 1;
    }
};


}

}//micro